home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Online / NNTPd / server / xhdr.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-11-01  |  4.2 KB  |  216 lines

  1. #ifndef lint
  2. static char    sccsid[] = "@(#)$Id: xhdr.c,v 1.13 1994/11/01 06:08:21 sob Exp sob $";
  3. #endif
  4.  
  5. #include "common.h"
  6.  
  7. #ifdef XHDR
  8.  
  9. #ifdef XOVER
  10. #include "xover.h"
  11. #endif
  12.  
  13. /* forward declarations */
  14. void print_header();
  15.  
  16. /*
  17.  * XHDR header [<messageid>|articlerange]
  18.  *
  19.  * header is a case-insensitive header field, minus any colons.
  20.  *
  21.  * articlerange is one of:
  22.  *    an article number
  23.  *    an article number followed by a dash to indicate all following
  24.  *    an article number followed by a dash followed by another
  25.  *        article number.
  26.  * e.g.,
  27.  * XHDR subject            retrieve subject of current article
  28.  * XHDR subject 5589-6325    retrieve subject of arts 5589 to 6325
  29.  * XHDR subject 5589-        retrieve subject of arts 5589 and up
  30.  * XHDR subject 5589        retrieve subject of art 5589 only
  31.  * XHDR subject <123@ucbvax>    retrieve subject of art <123@ucbvax>
  32.  *
  33.  * This command is an extension, and not included in RFC 977.
  34.  */
  35.  
  36. void
  37. xhdr(argc, argv)
  38.     int        argc;
  39.     char        *argv[];
  40. {
  41.     char        buf[MAXPATHLEN];
  42.     register int    artptr;
  43.     register int    artnum;
  44.     register int    low, high;
  45.     register FILE    *fp;
  46. #ifdef XOVER
  47.     int        over_candidate, hdr;
  48. #endif
  49.  
  50.     if (argc < 2 || argc > 3) {
  51.         printf("%d Usage: XHDR headerfield [artrange|<message-id>]\r\n",
  52.             ERR_CMDSYN);
  53.         (void) fflush(stdout);
  54.         return;
  55.     }
  56.  
  57.     if (!canread) {
  58.         printf("%d You only have permission to transfer, sorry.\r\n",
  59.             ERR_ACCESS);
  60.         (void) fflush(stdout);
  61.         return;
  62.     }
  63.  
  64.     /* Handle message-id requests */
  65.  
  66.     if (argc == 3 && *argv[2] == '<') {    /* Message ID */
  67.         fp = openartbyid(argv[2]);
  68.         if (fp == NULL) {
  69.             printf("%d No article by message-id %s, sorry.\r\n",
  70.                 ERR_NOART, argv[2]);
  71.             (void) fflush(stdout);
  72.             return;
  73.         }
  74.         printf("%d %d %s header of article %s.\r\n%s ",
  75.             OK_HEAD, group_artnum, argv[1], argv[2], argv[2]);
  76.         print_header(fp, argv[1]);
  77.         (void) fclose(fp);
  78.  
  79.         putline(".");
  80.         (void) fflush(stdout);
  81.         return;
  82.     }
  83.  
  84.     /*
  85.      * It must be a range of articles, which means that we need
  86.      * to be in a newsgroup already.
  87.      */
  88.  
  89.     if (!ingroup) {
  90.         printf("%d You are not currently in a newsgroup.\r\n",
  91.             ERR_NCING);
  92.         (void) fflush(stdout);
  93.         return;
  94.     }
  95.  
  96.     if (argc == 2) {
  97.         if (art_ptr < 0 || art_ptr >= num_arts) {
  98.             printf("%d No article is currently selected.\r\n",
  99.                 ERR_NOCRNT);
  100.             (void) fflush(stdout);
  101.             return;
  102.         }
  103.         high = low = art_array[art_ptr];
  104.         artptr = art_ptr;
  105.     } else {
  106.         register char *cp = index(argv[2], '-');
  107.         if (cp == NULL)
  108.             low = high = atoi(argv[2]);
  109.         else {
  110.             *cp++ = '\0';
  111.             low = atoi(argv[2]);
  112.             high = atoi(cp);
  113.             if (high < low)
  114.                 if (num_arts > 0)
  115.                     high = art_array[num_arts-1];
  116.                 else
  117.                     high = low;
  118.         }
  119.         artptr = 0;
  120.     }
  121.  
  122.     printf("%d %s fields follow\r\n", OK_HEAD, argv[1]);
  123.  
  124. #ifdef XOVER
  125.     if (over_is_cheap(low, high))
  126.         over_candidate = ((hdr = over_header(argv[1])) > 0);
  127.     else
  128.         over_candidate = 0;
  129. #endif
  130.     for (;artptr<num_arts; artptr++) {
  131.         if ((artnum = art_array[artptr]) < low)
  132.             continue;
  133.         if (artnum > high)
  134.             break;
  135.  
  136.         (void) sprintf(buf, "%d", artnum);
  137.         printf("%s ", buf);
  138. #ifdef XOVER
  139.         if (over_candidate) {
  140.             if (xfind(&over,artnum)) {
  141.                 over_grab_header(hdr, 1);
  142.                 continue;
  143.             } else if (over.num == -1)
  144.                 over_candidate = 0;
  145.         }
  146. #endif
  147.         fp = fopen(buf, "r");
  148.         if (fp == NULL)
  149.             continue;
  150.         print_header(fp, argv[1]);
  151.         (void) fclose(fp);
  152.     }
  153.  
  154.     putline(".");
  155.     (void) fflush(stdout);
  156. }
  157.  
  158.  
  159. void
  160. print_header(fp, header)
  161.     register FILE    *fp;
  162.     register char    *header;
  163. {
  164.     char        line[NNTP_STRLEN];
  165.     register char    *cp, *cp2;
  166.     register int    found = 0;
  167.  
  168.     cp = line;
  169.     while (fgets(line, sizeof line, fp) != NULL) {
  170.         if (cp && *line == '\n') {
  171.             break;
  172.         }
  173.         cp2 = cp;
  174.         cp = index(line, '\n');
  175.         if (!cp2 || isspace(*line)) {
  176.             if (found) {
  177.                 if (cp2) {
  178.                     for (cp2 = line+1; isspace(*cp2); cp2++)
  179.                         ;
  180.                     *--cp2 = ' ';
  181.                 }
  182.                 else
  183.                     cp2 = line;
  184.                 if (cp)
  185.                     *cp = '\0';
  186.                 printf("%s", cp2);
  187.             }
  188.         } else if (found)
  189.             break;
  190.         else if ((cp2 = index(line, ':')) != NULL) {
  191.             *cp2 = '\0';
  192.             if (strcasecmp(header, line) == 0) {
  193.                 cp2 += 2;
  194.                 if (cp)
  195.                     *cp = '\0';
  196.                 printf("%s", cp2);
  197.                 found = 1;
  198.             }
  199.         }
  200.     }
  201.     if (!found)
  202.         printf("(none)");
  203.     putchar('\r');
  204.     putchar('\n');
  205. }
  206.  
  207. #else /* !XHDR */
  208.  
  209. /* Kludge to get around Greenhills C compiler */
  210.  
  211. xhdr_greenkluydge()
  212. {
  213. }
  214.  
  215. #endif
  216.